<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\common\model;

/**
 * Description of RegistionModel
 *
 * @author DevPHP
 * @version 2018-4-10
 */
class RegistionModel {

    //put your code here
    public function registWithPassword() {
        
    }

    // 2018-4-10
    public function loginWithPassword($user_name, $password, $ip) {
        $data = ['login_id' => $user_name, 'password' => $password];
        // 登录验证
        $loginValidate = new \app\common\validate\LoginWithPasswordValidate();
        if (!$loginValidate->check($data)) {
            return fmt_result(false, $loginValidate->getError());
        }
        return $this->loginHandle($user_name, ['user_name' => $user_name], 'id, user_name, password, avatar_url');
    }

    public function loginWithSmscode($mobile, $smscode, $ip) {
        $data = ['login_id' => $mobile, 'sms_code' => $smscode];
        // 登录验证
        $loginValidate = new \app\common\validate\LoginWithSmscodeValidate();
        if (!$loginValidate->check($data)) {
            return fmt_result(false, $loginValidate->getError());
        }
        return $this->loginHandle($mobile, ['mobile' => $mobile], 'id, user_name, avatar_url');
    }

    // 2018-4-10
    private function registHandle($login_id, $where, $field) {
        if (BlacklistModel::check($login_id)) {
            return fmt_result(false, '黑名单帐号禁止注册');
        }
        if (BlacklistModel::check($ip)) {
            return fmt_result(false, '黑名单IP禁止注册');
        }
        if (intval(db('user_admin')->where($where)->count()) > 0) {
            return fmt_result(false, '账号已存在!');
        }
        $result = db('user_admin')->insert($data);
        if ($result > 0) {
            return fmt_result(false, '注册失败');
        }
        return fmt_result(true, '注册成功');
    }

}
